#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@company:ZYF
@author:yu.qian
@file:decorators.py
@time:2022/10/28
"""


class CustomizeError(Exception):
    """
    自定义异常基类
    """

    def __init__(self, *args):
        """
        构造函数

        :param args:
        """
        super(CustomizeError, self).__init__(*args)


class ApiError(CustomizeError):
    """
    标准API异常
    """

    def __init__(self, status, code, msg=None, *args, **kwargs):
        """
        标准API异常构造函数

        :param code: 异常编码
        :param msg: 异常信息
        :param args:
        :param kwargs:
        """
        if not isinstance(code, int):
            raise ValueError('"code" can only be integer.')
        if code == 0:
            raise ValueError('The code value cannot be equal to 0, 0 being reserved.')

        super(ApiError, self).__init__(status, code, msg, *args)

        self.status = -1
        self.code = code
        self.msg = msg
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return f'{self.__class__.__name__}(code={self.code}, msg={self.msg}, {self.args}, {self.kwargs})'

    @property
    def data(self):
        result = {'status': self.status, 'code': self.code, 'msg': self.msg}
        result.update(self.kwargs)
        return result
